<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="" prop="targetId">
        <el-input
          v-model="queryParams.targetId"
          clearable
          :placeholder="$t('proxy_log.inputId')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="">
        <el-button icon="el-icon-plus" size="mini" type="primary" @click="handleAddId">{{ $t('proxy_log.addId')}}</el-button>
      </el-form-item>
      <el-form-item label="">
        <el-date-picker
          v-model="dateRange"
          :default-time="['00:00:00', '23:59:59']"
          end-placeholder=""
          range-separator="-"
          start-placeholder=""
          style="width: 360px"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          :editable="false"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{ $t('proxy_log.query')}}</el-button>
      </el-form-item>
      <el-row v-if="isShowRecordId1">
        <el-button size="mini" type="success" @click="handleClickId(1)">{{ recordsId1 }}</el-button>
        <el-button icon="el-icon-delete" size="mini" type="warning" @click="handleRemove(1)"></el-button>
      </el-row>
      <el-row v-if="isShowRecordId2">
        <el-button size="mini" type="success" @click="handleClickId(2)">{{ recordsId2 }}</el-button>
        <el-button icon="el-icon-delete" size="mini" type="warning" @click="handleRemove(2)"></el-button>
      </el-row>
      <el-row v-if="isShowRecordId3">
        <el-button size="mini" type="success" @click="handleClickId(3)">{{ recordsId3 }}</el-button>
        <el-button icon="el-icon-delete" size="mini" type="warning" @click="handleRemove(3)"></el-button>
      </el-row>
      <el-row v-if="isShowRecordId4">
        <el-button size="mini" type="success" @click="handleClickId(4)">{{ recordsId4 }}</el-button>
        <el-button icon="el-icon-delete" size="mini" type="warning" @click="handleRemove(4)"></el-button>
      </el-row>
    </el-form>

    <div>
      <br>
      Total Obtained Quantity: {{ totalSendCount >= 0 ?  totalSendCount /100 : "-" }}
      <br>
      <br>
    </div>

    <el-table v-loading="loading" :data="logList">
      <el-table-column :label= "$t('proxy_log.id')" align="center" prop="targetId"></el-table-column>
      <el-table-column :label= "$t('proxy_log.name')" align="center" prop="targetName"/>
      <el-table-column :label= "$t('proxy_log.rewardNum')" align="center" prop="value">
        <template slot-scope="scope">
          <span>{{ scope.row.value / 100 }}</span>
        </template>
      </el-table-column>
      <el-table-column :label= "$t('proxy_log.proxyId')"  align="center" prop="franchiserId"/>
      <el-table-column :label= "$t('proxy_log.time')" align="center" prop="time">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template> -->
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {franchiserGetMarksIds, franchiserListRecord, franchiserMarksIds} from "@/api/system/record";

export default {
  name: "Log",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 玩家抽卡消耗表格数据
      logList: [],

      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        targetId: null,
        franchiserId: null,
        sliver: null,
        time: null
      },
      recordsIds: [], // 收藏的ids
      recordsId1: null,
      recordsId2: null,
      recordsId3: null,
      recordsId4: null,
      isShowRecordId1: false,
      isShowRecordId2: false,
      isShowRecordId3: false,
      isShowRecordId4: false,
      totalSendCount: -1,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        franchiserId: [
          {required: true, message: "分销商id不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.loadRecordIds();
    this.loading = false;
    // this.getList();
  },
  methods: {
    /** 查询玩家抽卡消耗列表 */
    getList() {
      franchiserListRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.logList = response.rows;
        this.totalSendCount = 0;
        for (let r of this.logList) {
          this.totalSendCount += r.value;
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        targetId: null,
        franchiserId: null,
        sliver: null,
        time: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if (!this.dateRange) {
        this.$modal.msgSuccess("Choose a date！");
        return;
      }
      if (this.dateRange.length != 2 || !this.dateRange[0] || !this.dateRange[1]) {
        this.$modal.msgSuccess("Choose a date！");
        return;
      }

      this.queryParams.pageNum = 1;
      this.getList();
    },

    handleAddId() {
      // this.loadRecordIds();
      if (this.recordsIds.length >= 4) {
        this.$modal.msgSuccess("Add up to 4 IDs");
        return;
      }
      if (!this.queryParams.targetId) {
        this.$modal.msgSuccess("Please enter the correct ID.");
        return;
      }
      for (let r of this.recordsIds) {
        if (r == this.queryParams.targetId) {
          this.$modal.msgSuccess("Please enter the correct ID.");
          return;
        }
      }


      let data = [];
      for (let r of this.recordsIds) {
        data.push(r);
      }
      data.push(this.queryParams.targetId);
      // this.recordsIds.push(this.queryParams.targetId);
      // this.$cache.local.set("marks_ids", JSON.stringify(this.recordsIds))
      // this.loadRecordIds();
      franchiserMarksIds({ids: JSON.stringify(data)}).then(response => {
        if (response.err_msg) {
          this.$modal.msgSuccess("Please enter the correct ID.");
        } else {
          this.loadRecordIds();
        }
      })
    },

    handleRemove(no) {
      // this.loadRecordIds();
      this.recordsIds.splice(no - 1, 1);
      // this.$cache.local.set("marks_ids", JSON.stringify(this.recordsIds));
      // this.loadRecordIds();
      franchiserMarksIds({ids: JSON.stringify(this.recordsIds)}).then(response => {
        if (response.err_msg) {
          this.$modal.msgSuccess("Please enter the correct ID.");
        } else {
          this.loadRecordIds();
        }
      })
    },

    handleClickId(no) {
      this.loadRecordIds();
      this.queryParams.targetId = this.recordsIds[no - 1];
    },

    loadRecordIds() {
      franchiserGetMarksIds().then(response => {
        this.recordsIds = JSON.parse(response.ids);
        this.recordsId1 = this.recordsIds.length >= 1 ? this.recordsIds[0] : null;
        this.recordsId2 = this.recordsIds.length >= 2 ? this.recordsIds[1] : null;
        this.recordsId3 = this.recordsIds.length >= 3 ? this.recordsIds[2] : null;
        this.recordsId4 = this.recordsIds.length >= 4 ? this.recordsIds[3] : null;
        this.isShowRecordId1 = this.recordsIds.length >= 1;
        this.isShowRecordId2 = this.recordsIds.length >= 2;
        this.isShowRecordId3 = this.recordsIds.length >= 3;
        this.isShowRecordId4 = this.recordsIds.length >= 4;
      })
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
};
</script>
